#include "manage.h"
#include"employee.h"
#include"employer.h"
#include"boss.h"

WorkerManager::WorkerManager()
{
    // 1.文件不存在
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    if (!ifs.is_open())
    {
        // cout << "文件不存在" << endl;
        // 初始化人数
        this->m_EmpNum = 0;
        // 初始化文件
        this->m_FileIsEmpty = true;
        // 初始化数组
        this->m_EmpArray = NULL;
        ifs.close();
        return;
    }
    // 2.文件存在数据为空
    char ch;
    ifs >> ch;
    if (ifs.eof())
    {
        // cout << "文件为空" << endl;
        // 初始化人数
        this->m_EmpNum = 0;
        // 初始化文件
        this->m_FileIsEmpty = true;
        // 初始化数组
        this->m_EmpArray = NULL;
        ifs.close();
        return;
    }

    // 3. 文件存在，并且记录数据
    int num = this->get_EmpNum();
    // cout << "职工的人数为: " << num << endl;
    this->m_EmpNum = num;

    // 开辟空间
    this->m_EmpArray = new Worker*[this->m_EmpNum];
    this->init_Emp();

    //cout data
    // for(int i =0; i < this->m_EmpNum;i++)
    // {
    //     cout << "职工编号: " << this->m_EmpArray[i]->m_Id
    //     << " 姓名: " << this->m_EmpArray[i]->m_Name
    //     << " 部门编号: " << this->m_EmpArray[i]->m_DeptId << endl;
    // }
}

void WorkerManager::showMenu()
{
    cout << "********************************************" << endl;
    cout << "*********  欢迎使用职工管理系统！ **********" << endl;
    cout << "*************  0.退出管理程序  *************" << endl;
    cout << "*************  1.增加职工信息  *************" << endl;
    cout << "*************  2.显示职工信息  *************" << endl;
    cout << "*************  3.删除离职职工  *************" << endl;
    cout << "*************  4.修改职工信息  *************" << endl;
    cout << "*************  5.查找职工信息  *************" << endl;
    cout << "*************  6.按照编号排序  *************" << endl;
    cout << "*************  7.清空所有文档  *************" << endl;
    cout << "********************************************" << endl;
    cout << endl;
}

void WorkerManager::exitSystem()
{
    cout << "欢迎下次使用" << endl;
    exit(0);
}

void WorkerManager::addEmp()
{
    // 提示输入添加人数
    cout << "请输入添加职工数量：" << endl;
    // 接收输入人数
    int addNum = 0;
    cin >> addNum;
    if (addNum <= 0)
    {
        cout << "输入有误，请重新输入" << endl;
        return;
    }
    else
    {
        // 计算新空间大小
        int newSize = this->m_EmpNum + addNum;
        // 开辟新空间
        Worker** newSpace = new Worker*[newSize];
        // 将原来空间下的数据拷贝到新空间中
        if (this->m_EmpArray != nullptr)
        {
            for (int i = 0; i < this->m_EmpNum; i++)
            {
                newSpace[i] = this->m_EmpArray[i];
            }
        }
        // 添加新数据
        for (int i = 0; i < addNum; i++)
        {
            int id;
            string name;
            int dSelect;
            cout << "请输入第" << i + 1 << "个新职工编号：" << endl;
            cin >> id;
            cout << "请输入第" << i + 1 << "个新职工姓名：" << endl;
            cin >> name;
            cout << "请选择该职工岗位：" << endl;
            cout << "1、普通职工" << endl;
            cout << "2、经理" << endl;
            cout << "3、老板" << endl;
            cin >> dSelect;

            Worker* worker = nullptr;

            switch (dSelect)
            {
            case 1:
                worker = new Employee(id, name, 1);
                break;
            case 2:
                worker = new Employer(id, name, 2);
                break;
            case 3:
                worker = new Boss(id, name, 3);
                break;
            default:
                break;
            }
            // 将创建职工职责保存到数组中
            newSpace[this->m_EmpNum + i] = worker;
        }

        // 释放原有空间
        if (this->m_EmpArray != nullptr) 
        {

        // delete[] this->m_EmpArray;
        this->m_EmpArray = nullptr;  // Prevent potential double-free errors
        }

        // 更改新空间的指向
        this->m_EmpNum = newSize;
        // 更新新职工人数
        this->m_EmpArray = newSpace;

        // 更新员工不为空的情况
        this->m_FileIsEmpty = false;
        // 提示添加成功
        cout << "成功添加" << addNum << "名新职工！" << endl;

        // 保存数据到文件中
        this->save();
    }
    // system("pause");
    // system("cls");
}

void WorkerManager::save()
{
    ofstream ofs;
    ofs.open(FILENAME, ios::out); // 输出的方式打开文件 --写文件
    // 将每一个人的数据写入文件中
    for (int i = 0; i < this->m_EmpNum; i++)
    {
        ofs << this->m_EmpArray[i]->m_Id << " "
            << this->m_EmpArray[i]->m_Name << " "
            << this->m_EmpArray[i]->m_DeptId << endl;
    }
    ofs.close();
}

// 统计文件中人数
int WorkerManager::get_EmpNum()
{
    ifstream ifs;
    ifs.open(FILENAME, ios::in);    // 打开文件，以读的方式

    int id;
    string name;
    int dId;
    int num = 0;
    while(ifs >> id && ifs >> name && ifs >> dId)
    {
        // 统计人数变量
        num++;
    }
    return num;
}

void WorkerManager::init_Emp()
{
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    int id;
    string name;
    int dId;
    int index = 0;
    while (ifs >> id && ifs >> name && ifs >> dId)
    {
        // 创建指针
        Worker * worker = NULL;
        if(dId ==1)
        {
            worker = new Employee(id,name,dId);
        }
        else if(dId ==2)
        {
            worker = new Employer(id,name,dId);
        }
        else
        {
            worker = new Boss(id,name,dId);
        }
        this->m_EmpArray[index] = worker;
        index++;
    }
    // close file
    ifs.close();
}

void WorkerManager::showEmp()
{
    if(this->m_FileIsEmpty)
    {
        cout << "file not exist or record is empty" << endl;
    }
    else
    {
        for(int i=0; i<m_EmpNum; i++)
        {
            // 调用多态接口
            this->m_EmpArray[i]->showInfo();
        }
    }
    // system("pause");
    // system("cls");
}

void WorkerManager::del_Emp()
{
    if(this->m_FileIsEmpty)
    {
        cout << "file not exist or record is empty" << endl;
    }
    else
    {
        // delete worker by id
        cout << "please enter the worker's id" << endl;
        int id = 0;
        cin >> id;
        int index = this->Is_exist(id);
        if(index!=-1)
        {
            // data move forward
            for(int i = index; i< this->m_EmpNum - 1; i++)
            {
                this->m_EmpArray[i] = this->m_EmpArray[i+1];
            }
            this->m_EmpNum--;
            this->save();
            cout << "delete successfully" << endl;
        }
        else
        {
            cout << "delete fail" << endl;
        }
    }
    // system("pause");
    // system("cls");
}

int WorkerManager::Is_exist(int id)
{
    for(int i = 0;i < this->m_EmpNum;i++)
    {
        if(this->m_EmpArray[i]->m_Id == id)
        {
            // find worker
            return i;
        }
    }
    return -1;
}

void WorkerManager::Mod_Emp()   // modify worker
{
    if(this->m_FileIsEmpty)
    {
        cout << "file not exist or record is empty" << endl;
    }
    else
    {
        cout << "please enter the worker id" << endl;
        int id;
        cin >> id;
        int ret = this->Is_exist(id);
        if(ret!=-1)
        {
            delete this->m_EmpArray[ret];
            int newId = 0;
            string newName = "";
            int dSelect = 0;
            cout << "find worker, please enter new id" << endl;
            cin >> newId;
            cout << "please enter new name" << endl;
            cin >> newName;
            cout << "please enter new position" << endl;
            cout << "1. employee" << endl;
            cout << "2. employer" << endl;
            cout << "3. boss" << endl;
            cin >> dSelect;
            Worker* worker = NULL;
            switch (dSelect)
            {
            case 1:
                worker = new Employee(newId, newName, dSelect);
                break;
            case 2:
                worker = new Employer(newId, newName, dSelect);
                break;
            case 3:
                worker = new Boss(newId, newName, dSelect);
                break;
            default:
                break;
            }
            this->m_EmpArray[ret] = worker;
            cout << "modify successfully" << endl;
            this->save();
        }
        else
        {
            cout << "modify fail," << "worker not exist" << endl;
        }
    }
}

// 查找职工
void WorkerManager::Find_Emp()
{
    if(this->m_FileIsEmpty)
    {
        cout << "file not exist or record is empty" << endl;
    }
    else    // 通过编号查找或者通过姓名查找
    {
        cout << "please enter the worker id or name" << endl;
        cout << "1. search by id" << endl;
        cout << "2. search by name" << endl;
        int select = 0;
        cin >> select;
        if(select ==1)
        {
            int id;
            cout << "please enter the worker id" << endl;
            cin >> id;
            int ret = this->Is_exist(id);
            if(ret!=-1)
            {
                cout << "find worker" << endl;
                this->m_EmpArray[ret]->showInfo();
            }
            else
            {
                cout << "worker not exist" << endl;
            }
        }
        else if(select ==2)
        {
            string name;
            cout << "please enter the worker name" << endl;
            cin >> name;
            bool flag = false;  // flag to check if worker exist
            for(int i = 0; i < this->m_EmpNum; i++)
            {
                if(this->m_EmpArray[i]->m_Name == name)
                {
                    cout << "find worker" << endl;
                    this->m_EmpArray[i]->showInfo();
                    flag = true;
                }
            }
            if(flag == false)   // if worker not exist
            {
                cout << "find fail,and worker not exist" << endl;
            }
        }
        else
        {
            cout << "input error" << endl;
        }
        
    }

}

void WorkerManager::Sort_Emp()
{
    if(this->m_FileIsEmpty)
    {
        cout << "file not exist or record is empty" << endl;
    }
    else
    {
        cout << "please choose sort method" << endl;
        cout << "1. ascending" << endl;
        cout << "2. descending" << endl;
        int select = 0;
        cin >> select;
        for(int i = 0; i < this->m_EmpNum; i++)
        {
            int minOrMax = i;   // assume the first one is the smallest or the biggest
            for(int j = i+1; j < this->m_EmpNum; j++)
            {
                if(select == 1) // ascending
                {
                    if(this->m_EmpArray[minOrMax]->m_Id > this->m_EmpArray[j]->m_Id)
                    {
                        minOrMax = j;
                    }
                }
                else    // descending
                {
                    if(this->m_EmpArray[minOrMax]->m_Id < this->m_EmpArray[j]->m_Id)
                    {
                        minOrMax = j;
                    }
                }
            }
            if(i!=minOrMax)
            {
                Worker* temp = this->m_EmpArray[i]; 
                this->m_EmpArray[i] = this->m_EmpArray[minOrMax];
                this->m_EmpArray[minOrMax] = temp;
            }
        }
        cout << "sort successfully" << endl;
        this->save();
        this->showEmp();
    }
}

void WorkerManager::Clear_Emp()
{
    cout << "are you sure to clear all workers?" << endl;
    cout << "1. yes" << endl;
    cout << "2. no" << endl;
    cout << "please enter your choice" << endl;
    int select = 0;
    cin >> select;
    if(select == 1)
    {
        ofstream ofs;
        ofs.open(FILENAME, ios::trunc); // delete the file and create a new file
        ofs.close();

        if (this->m_EmpArray != NULL)
        {
            for(int i=0; i< this->m_EmpNum; i++)
            {
                if(this->m_EmpArray[i]!=NULL)
                {
                    delete this->m_EmpArray[i];    
                    this->m_EmpArray[i] = NULL;    
                }
            }
            delete []this->m_EmpArray;
            this->m_EmpArray = NULL;
            this->m_EmpNum = 0;
            this->m_FileIsEmpty = true;
        }
    else
    {
        cout << "clear fail" << endl;
    }
    
    }
}


WorkerManager::~WorkerManager()
{
    if (this->m_EmpArray != NULL)
    {
        for(int i=0; i< this->m_EmpNum; i++)
        {
            if(this->m_EmpArray[i]!=NULL)
            {
                delete this->m_EmpArray[i];    
                this->m_EmpArray[i] = NULL;    
            }
        }
        delete []this->m_EmpArray;
        this->m_EmpArray = NULL;
    }
}
